<script>
    //    $(document).ready(function () {
    ////        $n=0;
    ////        while($n<12){
    ////            calendar($n++);
    //////            calendar(1);
    ////        }
    //
    //    });

    var cDate = new Date();
    var month = cDate.getMonth();
    var year = cDate.getFullYear();
    var nowDate = new Date();
    var globalObj;//指向当前日历被点击的标签

    // 定义酒店，sku id全局变量
    var goods_id;
    var sku_id;
//    var form_type;



    //获取单前被点击的对象
    function setCal(obj) {
        globalObj = obj;
        //TODO 在重新设置值的时候将数据显示在莫泰框中
//        $(globalObj).children('font.price.adult').html();
//        $(globalObj).children('font.store.adult').html();
        $("#price1").val(parseInt($(globalObj).children('font.price.adult').html()));
        $("#stock1").val(parseInt($(globalObj).children('font.store.adult').html()));

//        $(globalObj).children('font.price.child').html();
//        $(globalObj).children('font.store.child').html();
        $("#price2").val(parseInt($(globalObj).children('font.price.children').html()));
        $("#stock2").val(parseInt($(globalObj).children('font.store.children').html()));

//        $(globalObj).children('font.price.diff').html();
//        $(globalObj).children('font.store.diff').html();
        $("#price3").val(parseInt($(globalObj).children('font.price.diff').html()));
        $("#stock3").val(parseInt($(globalObj).children('font.store.diff').html()));
//        $(globalObj).children('input[name="c_price"]').val($price);
//        $(globalObj).children('input[name="c_stock"]').val($stock);
//        $("#modalClick").click();
    }

    /**
     * 设置价格日历的价格
     */

    function getInfo() {
        $price1=$("#price1").val();
        $store1=$("#stock1").val();
        if ($price1 <= 0) {
            tip.msgbox.err('价格值大于零');
            return;
        } else if ($price1 % $price1 != 0) {
            tip.msgbox.err('请输入数字');
            return;
        }

        if ($store1 <0) {
            tip.msgbox.err('库存值不能小于零');
            return;
        } else if ($store1 % $store1 != 0) {
            tip.msgbox.err('请输入数字');
            return;
        }

        $price2=$("#price2").val();
        $store2=$("#stock2").val();
        if ($price2 <= 0) {
            tip.msgbox.err('价格值大于零');
            return;
        } else if ($price2 % $price2 != 0) {
            tip.msgbox.err('请输入数字');
            return;
        }

        if ($store2 <0) {
            tip.msgbox.err('库存值不能小于零');
            return;
        } else if ($store2 % $store2 != 0) {
            tip.msgbox.err('请输入数字');
            return;
        }
        $price3=$("#price3").val();
        $store3=$("#stock3").val();
        if ($price3 <= 0) {
            tip.msgbox.err('价格值大于零');
            return;
        } else if ($price3 % $price3 != 0) {
            tip.msgbox.err('请输入数字');
            return;
        }

        if ($store3 <0) {
            tip.msgbox.err('库存值不能小于零');
            return;
        } else if ($store3 % $store3 != 0) {
            tip.msgbox.err('请输入数字');
            return;
        }

        $(globalObj).children('font.price.adult').html($price1+'￥');
        $(globalObj).children('font.store.adult').html( $store1+'件');


        $(globalObj).children('font.price.children').html($price2+'￥');
        $(globalObj).children('font.store.children').html($store2+'件');

        $(globalObj).children('font.price.diff').html($price3+'￥');
        $(globalObj).children('font.store.diff').html($store3+'件');


        //数据库回显到莫泰框
//        $(globalObj).children('font.price.adult').html($price+'￥');
//        $(globalObj).children('font.store.adult').html($stock + '件');
//        $(globalObj).children('input[name="c_price"]').val($price);
//        $(globalObj).children('input[name="c_stock"]').val($stock);

//        $actObj.next().html($price+'￥');
//        $actObj.next().next().next().html($stock + '件');
//        $actObj.next().next().next().val($price);
//        $actObj.next().next().next().next().val($stock);


        $date=$(globalObj).children('input[name="c_date"]').val();
        goods_id=$("input[name='goods_id']").val();
//        form_type=$("input[name='form_type']").val();

//        //alert($form_type);
        //异步保存价格日历信息
        var data={"price1":$price1,"stock1":$store1,
            "price2":$price2,"stock2":$store2,
            "price3":$price3,"stock3":$store3,
            "goods_id":goods_id, "pdate":$date};
        console.log(data);
        $.post('{php echo webUrl("goods/updateCalendarPriceLine")}',data,function (data) {
            $("#myModal").modal('hide');
            console.log(data);
            data=JSON.parse(data)
            $("#err_msg").html(data);
//            //alert(data.status);
            if (data.status==1)
                tip.msgbox.suc('更新成功！');
            else
                tip.msgbox.err('更新失败！');
        });

// $(globalObj).text($stock)
    }

    /**
     * 生成日历
     * @param act 表示获取的月份 0当月，-1前一月，1下一月 其他数表示向后推几个月
     */
    function calendar(act,price) {
        ////console.log(act+'>'+price[0].pdate);
        month = cDate.getMonth();
        cDate = new Date();
        year = cDate.getFullYear();
        month += act;

        if (month == 12) {
            year += 1;
            month = 0;
        } else if (month == -1) {
            year -= 1;
            month = 11;
        }
        var firstDate = new Date(year, month, 1);
        nowDate=firstDate;//更新单前时间目前日历显示的时间
        $week = firstDate.getDay();
        $week = $week == 0 ? 7 : $week;
        var startDate = new Date(year, month, 1);
        startDate.setDate(startDate.getDate() - $week);


        $strHeard = '<table class="table table-bordered">';
        $strTd = '<tr>\n' +
            '        <th>星期一</th>\n' +
            '        <th>星期二</th>\n' +
            '        <th>星期三</th>\n' +
            '        <th>星期四</th>\n' +
            '        <th>星期五</th>\n' +
            '        <th>星期六</th>\n' +
            '        <th>星期天</th>\n' +
            '    </tr>';
        for ($i = 1; $i < 42;) {
            $strTd += '<tr>';
            for ($j = 0; $j < 7; $j++, $i++) {


                startDate.setDate(startDate.getDate() + 1);
                $Fulldate=startDate.getFullYear()+'-';
                $FullM=(startDate.getMonth()+1)>=10?(startDate.getMonth()+1):'0'+(startDate.getMonth()+1);
                $FullD=startDate.getDate()>=10?startDate.getDate():'0'+startDate.getDate();
                $Fulldate=$Fulldate+$FullM+'-'+$FullD;
                if (startDate.getMonth() == nowDate.getMonth()) {
                    $n=0;
                    $priceInfo="";
                    while ($n++<3){//三种SKU
                        $toDay=getNowPrice(price,$Fulldate,$n);
                        if ($toDay==false){
                            if ($n == 1) {
                                $priceInfo += '<font class="skuname">成人:</font><font color="red" class="price adult"></font><font class="store adult"></font><br>';
                            } else if ($n == 2) {
                                $priceInfo += '<font class="skuname">儿童:</font><font color="red" class="price children"></font><font class="store children"></font><br>';
                            } else if ($n == 3) {
                                $priceInfo += '<font class="skuname">单房差:</font><font color="red" class="price diff"></font><font class="store diff"></font><br>';
                            }

//                         //console.log('toDayValue'+$toDay);
                        }else {
//                         //console.log('toDayValue'+$toDay);
                            $toDayPrice=$toDay.PRICE+'￥';
                            if ($toDay.STORE==-1)
                                $toDayStore='充足';
                            else $toDayStore=$toDay.STORE+'件';
                            if ($n==1){

                                $priceInfo+='<font class="skuname">成人:</font><font color="red" class="price adult">'+$toDayPrice+'</font><font class="store adult">'+$toDayStore+'</font><br>';
                            }else if($n==2){
                                $priceInfo+='<font class="skuname">儿童:</font><font color="red" class="price children">'+$toDayPrice+'</font><font class="store children">'+$toDayStore+'</font><br>';
                            }else if($n==3){
                                $priceInfo+='<font class="skuname">单房差:</font><font color="red" class="price diff">'+$toDayPrice+'</font><font class="store diff">'+$toDayStore+'</font><br>';

                            }

                        }
                    }

//                $toDay==false?''://alert('toDay'+$toDay.price);
                        if (startDate.getDate() == cDate.getDate()) {
                            $strTd += '<td data-toggle="modal" data-target="#myModal" onclick="setCal(this)"><font color="red" style="font-family: Arial Black">' + startDate.getDate() +
                                '</font><br>'+$priceInfo+
                                '<input type="hidden" name="c_price1"><input type="hidden" name="c_stock1">' +
                                '<input type="hidden" name="c_price2"><input type="hidden" name="c_stock2">' +
                                '<input type="hidden" name="c_price3"><input type="hidden" name="c_stock3">' +
                                '<input type="hidden" name="c_date" value="' + $Fulldate + '"></td>';
                            continue;
                        }
                        $strTd += '<td data-toggle="modal" data-target="#myModal" onclick="setCal(this)"><font style="font-family: Arial Black">' + startDate.getDate() + '</font><br>'+$priceInfo+
                            '<input type="hidden" name="c_price1"><input type="hidden" name="c_stock1">' +
                            '<input type="hidden" name="c_price2"><input type="hidden" name="c_stock2">' +
                            '<input type="hidden" name="c_price3"><input type="hidden" name="c_stock3">' +
                            '<input type="hidden" name="c_date" value="' + $Fulldate + '"></td>';
                        continue;


                }
                $strTd += '<td ></td>';
//                $strTd += '<td onclick=""><font>' + startDate.getDate() + '日</font><br><font></font><font></font><input type="hidden" name="price"><input type="hidden" name="stock"><input type="hidden" name="date" value="' + $Fulldate + '"></td>';

            }
            $strTd += '</tr>';
        }
        $strHeard += $strTd + '</table>';

        $calendarHead = '    <table class="table table-striped">\n' +
            '        <tr>\n' +
            '            <td><button type="button" class="btn btn-primary btn-lg btn-block">' + firstDate.getFullYear() + '年' + (firstDate.getMonth() + 1) + '月' + cDate.getDate() + '日</button></td>\n' +
            '        </tr>\n' +
            '    </table>';
        $("#calendar").append($calendarHead + $strHeard);
//        $("#calendar").html($calendarHead + $strHeard);

    }

    /**
     * 获取当天价格日历信息对象
     * @param priceList 这个月的价格日历信息
     * @param pdate 成功返回价格对象
     * @returns {boolean} 当天没有价格信息返回false
     */
    function getNowPrice(priceList,pdate,sku){
        var toDay=false;
        $.each(priceList,function (key,item) {

//            console.log(JSON.stringify(item));
            if (item.PDATE==pdate && sku==item.GROUPID){
//                //alert(JSON.stringify(priceList))
//                //console.log(JSON.stringify(priceList));
//                //console.log(item.pdate+'=='+pdate+'--'+(item.pdate==pdate));
//                //alert(p.pdate)
                toDay=item;
                return;
            }
        })
        return toDay;

    }

    /**
     * 获取这件商品往后一年的价格日历信息
     * 并生成价格日历信息
     */
    function getp(){
        $("#calendar").html('');
        goods_id=$("input[name='goods_id']").val();
//        goods_id=187;

        $data={"goods_id":goods_id,"mCount":12};
//        //alert($data);
        $.post('{php echo webUrl("goods/getPriceCalendarLine")}',$data,function(data){

//            console.log(data);
            data=JSON.parse(data);
//            //console.log(data);
//            //alert(data.status);
//            //alert(JSON.parse(data).result.list);
//            //console.log(JSON.parse(data).result.list);
//            //console.log(JSON.parse(data).result.list[1]);
//            //console.log(JSON.parse(data));

//            //alert(JSON.parse(JSON.parse(data).result.list));
            if (data.status=='0'){
                tip.msgbox.err('暂无数据!');
            }else{
                $priceList=JSON.parse(data.result.list);
//                console.log($priceList);
//                console.log($priceList[0]);
//                console.log($priceList[0][0].GOODSID);

                $m=0;
//                alert($priceList);
//                alert($priceList);
//                alert(JSON.stringify($priceList[0]));
//                alert(JSON.stringify($priceList[1]));
                while($m++<12){
                    calendar($m-1,$priceList[$m-1]);
//            calendar(1);
                }
            }
        });
    }
</script>
<div style="overflow-y:scroll;width: 100%;height: 100%;">
    <!-- 模态框（Modal） -->
    <!--<a class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal" style="display: block;" id="modalClick"></a>-->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                        &times;
                    </button>
                    <h4 class="modal-title" id="myModalLabel">
                        设置当天的价格和库存
                    </h4>
                </div>
                <div class="modal-body">
                    <!--<form class="form-horizontal form-validate">-->
                    <div class="form-group">
                        <label class="col-sm-2 control-label">成人</label>
                        <div class="col-sm-9 col-xs-12" style="margin-top: 20px;margin-bottom: 20px;">
                            价格:<input type="number" id="price1" class="form-control"/>
                            库存:<input type="number" id="stock1" class="form-control"/>

                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">儿童:</label>
                        <div class="col-sm-9 col-xs-12" style="margin-top: 20px;margin-bottom: 20px;">
                            价格:<input type="number" id="price2" class="form-control"/>
                            库存:<input type="number" id="stock2" class="form-control"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">单房差:</label>
                        <div class="col-sm-9 col-xs-12" style="margin-top: 20px;margin-bottom: 20px;">
                            价格:<input type="number" id="price3" class="form-control"/>
                            库存:<input type="number" id="stock3" class="form-control"/>
                        </div>
                    </div>
                    <!--<div class="form-group">-->

                        <!--<label class="col-sm-2 control-label">库存</label>-->
                        <!--<div class="col-sm-9 col-xs-12">-->
                            <!--<input type="number" id="stock" class="form-control"/>-->
                        <!--</div>-->

                    <!--</div>-->

                    <!--</form>-->
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                    </button>
                    <button type="button" class="btn btn-primary" onclick="getInfo(this)">
                        提交更改
                    </button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal -->
    </div>

</div>